home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7336 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  65 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Help] I can`t find m
  5. Date: 25 Feb 1996 09:08:27 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4gp8ub$mcs@gail.ripco.com>
  8. NNTP-Posting-Host: golden.ripco.com
  9.  
  10. etoivane@direct.ca (Ed Toivanen) in reply to the message
  11. <4ggvgr$1b2@aurora.engr.LaTech.edu> wrote in
  12. <4givk5$bqk@aphex.direct.ca> the code which I have revised below.
  13.  
  14. As usual, my changes and comments are marked with `/* mha - ... */',
  15. and, as usual, not all changes reflect actual errors in the code.
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. #define NAMESIZE 100
  21. #define BUFSIZE  4
  22.  
  23. int main(void)
  24. {
  25.     char buf[BUFSIZE];
  26.     char name[NAMESIZE];
  27.     int age, next_age = 1;      /* mha - was `int age, next_age;',
  28.                                  * which leaves next_age unintialized
  29.                                  * before the 'next_age += age;'
  30.                                  * statement */
  31.  
  32.     printf("Please enter your name:\t");
  33.     fflush(stdout);             /* mha - added.  Make sure the above
  34.                                  * appears in stdout before the
  35.                                  * fgets() below. */
  36.     fgets(name, sizeof name, stdin);    /* mha - was `name = fgets(buf,
  37.                                          * sizeof(buf), stdin);', but
  38.                                          * `name' is an array[NAMESIZE]
  39.                                          * of char, not a pointer to
  40.                                          * char.  You might consider
  41.                                          * some error checking here.  */
  42.     printf("\nPlease enter your age:\t");
  43.     fflush(stdout);             /* mha - added */
  44.     fgets(buf, sizeof buf, stdin);  /* mha - moved fgets() out of
  45.                                      * atoi() call.  Now it's your job
  46.                                      * to add some error checking. */
  47.     age = atoi(buf);            /* mha - some error checking on the
  48.                                  * legality of the contents of buf
  49.                                  * would be nice. (Your job) */
  50.     next_age += age;            /* mha - (unchanged)
  51.                                  * Of course, you could just
  52.                                  * increment age (`age++;'), and
  53.                                  * replace `next_age' with `age' in the
  54.                                  * printf() following. Or you could, if
  55.                                  * you need to have both these
  56.                                  * variables, have `next_age = age+1;' */
  57.     printf("\nHi, %s ,next year, you will be %d\n", name, next_age);
  58.     return 0;                   /* mha - note: parens around `0'
  59.                                  * unneeded */
  60. }
  61.                                                                  
  62. --
  63. * Martin Ambuhl       net: mambuhl@ripco.com
  64. * Chicago, IL (USA)    
  65.